﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls.Primitives;
using System.Windows;
using System.Diagnostics;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Interop;
using System.Windows.Controls;

namespace WpfApplication2
{
    public class MouseDownThumb : Thumb
    {
        private Stopwatch stopWatch = new Stopwatch();

        private const int WM_LBUTTONDOWN = 0x0201;
        private const int WM_LBUTTONUP = 0x0202;

        //private double dCanvasLeft = 0;
        //private double dCanvasTop = 0;

        public event RoutedEventHandler MouseDownClick;
        public event MouseButtonEventHandler MyMouseLeftButtonDown;
        public event MouseButtonEventHandler MyMouseLeftButtonUp;

        private bool isMouseDown = false;

        public int Index
        {
            get;
            set;
        }

        public FrameworkElement ParentControl
        {
            get;
            set;
        }

        public string strTitle
        {
            get;
            set;
        }

        public bool bIsDraged = false;

        public RectangleGeometry RectangleGeometry
        {
            get;
            set;
        }

        public MouseDownThumb()
        {
            this.Loaded += new System.Windows.RoutedEventHandler(MouseDownThumb_Loaded);
            //this.AddHandler(Thumb.MouseLeftButtonDownEvent, new MouseButtonEventHandler(Button_MouseDown), true);

            //this.MouseLeftButtonUp += MouseDownThumb_MouseLeftButtonUp;

            

            //使用自定义的ControlTemplate
            this.Template = (ControlTemplate)App.Current.FindResource("ButtonTemplate");

            //在ControlTemplate中找控件最重要的就是让控件初始化到visualtree上，
            //所以如何没有找到ControlTemplate的控件就有可能是控件还未能加载到visualtree。
            //可以用一下两种方式强制刷新模板控件至visualtree.
            //第一种是UpdateLayout()  方法。
            this.UpdateLayout();               
        }

        //void MouseDownThumb_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        //{
        //    //throw new NotImplementedException();
        //    System.Diagnostics.Debug.WriteLine("点击了：" + strTitle);
        //}        

        //void Button_MouseDown(object sender, MouseButtonEventArgs e)
        //{
        //    e.Handled = false;

        //    //Button tb = (Button)this.Template.FindName("Button", this);
        //    //tb.Content = strTitle;
        //}

        void MouseDownThumb_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
            if (source != null)
            {
                source.AddHook(WindowProc);
            }

            //主动设置显示的内容
            Button tb = (Button)this.Template.FindName("Button", this);
            tb.Content = strTitle;
        }

        /// <summary>
        /// 消息过滤监视
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="msg"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <param name="handled"></param>
        /// <returns></returns>
        private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            switch (msg)
            {
                case WM_LBUTTONDOWN:
                    {
                        if (ParentControl != null)
                        {
                            //dCanvasLeft = Canvas.GetLeft(ParentControl);
                            //dCanvasTop = Canvas.GetTop(ParentControl);
                            stopWatch.Reset();
                            stopWatch.Start();
                            Point point = Mouse.GetPosition(ParentControl);
                            if (MyMouseLeftButtonDown != null && RectangleGeometry.FillContains(point))
                            {
                                MyMouseLeftButtonDown(this, new MouseButtonEventArgs(InputManager.Current.PrimaryMouseDevice, 0, MouseButton.Left));
                                isMouseDown = true;
                            }
                        }
                    }
                    break;
                case WM_LBUTTONUP:
                    {
                        if (ParentControl != null)
                        {
                            if (isMouseDown == false)
                            {
                                //不是自身触发的
                            }
                            else
                            {
                                stopWatch.Stop();                                

                                if (this.IsEnabled && bIsDraged == false && stopWatch.ElapsedMilliseconds < 333)
                                {
                                    System.Diagnostics.Debug.WriteLine(strTitle + " 单击");
                                    //认为是点击
                                    if (MouseDownClick != null)
                                    {
                                        MouseDownClick(this, null);
                                    }
                                }
                                else
                                {
                                    System.Diagnostics.Debug.WriteLine(strTitle + " 拖动");
                                }

                                //鼠标抬起就认为不再拖动了
                                bIsDraged = false;

                                if (MyMouseLeftButtonUp != null && isMouseDown)
                                {
                                    MyMouseLeftButtonUp(this, new MouseButtonEventArgs(InputManager.Current.PrimaryMouseDevice, 0, MouseButton.Left));
                                    isMouseDown = false;
                                }

                                //handled = true;
                                //return (IntPtr)1;
                                break;

                                //Point point = Mouse.GetPosition(this);
                                //if (this.IsEnabled /*&& dCanvasLeft == Canvas.GetLeft(ParentControl) && dCanvasTop == Canvas.GetTop(ParentControl)*/ && stopWatch.ElapsedMilliseconds < 500)
                                //{
                                //    System.Diagnostics.Debug.WriteLine(strTitle + " 单击");
                                //    //认为是点击
                                //    if (MouseDownClick != null)
                                //    {
                                //        MouseDownClick(this, null);
                                //    }
                                //}
                                //else
                                //{
                                //    System.Diagnostics.Debug.WriteLine(strTitle + " 拖动");
                                //}


                                //if (MyMouseLeftButtonUp != null && ((point.X < RectangleGeometry.Rect.Width && point.X > 0 && point.Y < RectangleGeometry.Rect.Height && point.Y > 0) || isMouseDown))
                                //{
                                //    MyMouseLeftButtonUp(this, new MouseButtonEventArgs(InputManager.Current.PrimaryMouseDevice, 0, MouseButton.Left));
                                //    isMouseDown = false;
                                //}
                            }
                        }
                    }
                    break;
            }

            return (IntPtr)0;
        }    
    }
}
